package hmvc4gwt.framework.client.view;

import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.Widget;

import hmvc4gwt.client.event.IEvent;
import hmvc4gwt.client.view.AView;
import hmvc4gwt.framework.client.events.ResetViewEvent;

public abstract class GuiView extends AView {
	
	private final Panel mainPanel = new AbsolutePanel();

	public final void add(Widget child) {
		mainPanel.add(child);
	}

	public final void clear() {
		mainPanel.clear();
	}
	
	public void init() {
		mainPanel.setStyleName("mainPanel");

		getController().register(ResetViewEvent.class, this);
		
		initImpl();
	}

	public abstract void initImpl();

	public Widget getWidget() {		
		return mainPanel;
	}
	
	public boolean remove(Widget child) {
		return mainPanel.remove(child);
	}
	
	public final void handleEvent(IEvent event) {
		
		if (event instanceof ResetViewEvent) {
			resetView();
		}
		
		handleEventImpl(event);
	}
	
	public abstract void handleEventImpl(IEvent event);
	
	/**
	 * Overwrite this Method if you need the to reset this View.
	 * This Method will be called if the View receives an ResetViewEvent()
	 */
	protected void resetView() {
		
	}

}
